using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ECBS_Backend.dao;
using ECBS_Backend.model;
using Microsoft.AspNetCore.Mvc;
using ECBS_Backend.service;
using ECBS_Backend.utils;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ECBS_Backend.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AdminController : ControllerBase
    {

        private readonly AdminService adminService;

        public AdminController(AdminService adminService)
        {
            this.adminService = adminService;
        }

        /// <summary>
        /// 管理员登陆
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        [HttpGet("login")]
        public ActionResult<string> Login([FromQuery]string userName, [FromQuery]string password)
        {
            try{
                Admin a =  adminService.AdminLogin(userName, password);
                string token = JwtGenerator.GenerateJWT("Admin", userName);

                WeIDToken weIDToken = new WeIDToken { WeID = a.UserName, Token = token };

                return Ok(new { message = "登陆成功", data = weIDToken, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        
        /// <summary>
        /// 超级管理员添加学校，注册权威证书发行者
        /// </summary>
        [HttpPost("addSchool")]
        [Authorize(Roles = "Admin")]
        public ActionResult<School> AddandVerifySchool([FromBody]School school, [FromQuery]string adminWeID)
        {
            try{
                School s = adminService.AddandVerifySchool(school, adminWeID);
                return Ok(new { message = "添加注册学校成功", data = s, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 将学校认证为权威证书发行者
        /// </summary>
        /// <param name="schoolWeID"></param>
        /// <returns></returns>
        [HttpPost("recognizeSchool")]
        [Authorize(Roles = "Admin")]
        public ActionResult<bool> RecognizeSchool([FromQuery]string schoolWeID)
        {
            try
            {
                bool res = adminService.RecognizeSchool(schoolWeID);
                return Ok(new { message = "认证学校成功", data = res, code = 200 });
            } catch(Exception e)
            {
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 撤销学校权威证书发行者
        /// </summary>
        /// <param name="schoolWeID"></param>
        /// <returns></returns>
        [HttpPost("deRecognizeSchool")]
        [Authorize(Roles = "Admin")]
        public ActionResult<bool> DeRecognizeSchool([FromQuery]string schoolWeID)
        {
            try
            {
                bool res = adminService.DeRecognizeSchool(schoolWeID);
                return Ok(new { message = "撤销学校成功", data = res, code = 200 });
            } catch(Exception e)
            {
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 超级管理员添加公司
        /// </summary>
        [HttpPost("addEnterprise")]
        [Authorize(Roles = "Admin")]
        public ActionResult<Enterprise> AddEnterprisse([FromBody]Enterprise enterprise)
        {
            try{
                Enterprise e = adminService.AddEnterprise(enterprise);
                return Ok(new { message = "添加企业成功", data = e, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 超级管理员查看企业
        /// </summary>
        [HttpGet("displayEnterprise")]
        [Authorize(Roles = "Admin")]
        public ActionResult<dynamic> DisplayEnterprise([FromQuery]string keyword){
            try
            {
                var e=adminService.DisplayEnterprise(keyword);
                return Ok(new { message = "查看企业成功", data = e, code = 200 });
            }
            catch (Exception e)
            {
                 return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 超级管理员查看学校
        /// </summary>
        [HttpGet("displaySchool")]
        public ActionResult<dynamic> DisplaySchool([FromQuery]string keyword){
            try
            {
                var s=adminService.DisplaySchool(keyword);
                return Ok(new { message = "查看学校成功", data = s, code = 200 });
            }
            catch (Exception e)
            {
                 return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 删除企业
        /// </summary>
        /// <param name="WeID"></param>
        [HttpDelete("deleteEnterprise")]
        [Authorize(Roles = "Admin")]
        public ActionResult<string> DeleteEnterprise([FromQuery] string WeID)
        {
            try
            {
                Enterprise e = adminService.DeleteEnterprise(WeID);
                return Ok(new { message = "删除企业成功", data = e.UserName, code = 200 });
            }
            catch (Exception e)
            {
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }
    }
}
